﻿using System;
using System.Linq;
using System.ComponentModel.Composition;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace Cubicle.Core.Config
{
    [Serializable]
    [PartCreationPolicy(CreationPolicy.Shared)]
    [XmlRoot(ElementName = "config")]
    public class EngineConfig : BaseConfig
    {
        [Export]
        public static EngineConfig Create
        {
            get { return Read<EngineConfig>(); }
        }

        [XmlElement(ElementName = "queue")]
        public List<QueueConfig> Queues;

        [XmlElement(ElementName = "add")]
        public List<KvpElement> Parameters = new List<KvpElement>();

        public T GetParameter<T>(string key, T failValue)
        {
            var result = Parameters.FirstOrDefault(x => x.Key == key);
            return result == null 
                ? failValue 
                : result.Get<T>();
        }

        [XmlIgnore]
        public string Repository { get { return GetParameter("repository", "ExcelRepository"); } }

        [XmlIgnore]
        public string QueueName { get { return GetParameter("queueName", "default"); } }

        [XmlIgnore]
        public string ScriptFile { get { return GetParameter("scriptFile", (string)null); } }

        [XmlIgnore]
        public string HandleRegister { get { return GetParameter("handleRegister", "HandleRegister"); } }

        [XmlIgnore]
        public string HostName { get { return GetParameter("hostName", "defaultHost"); } }


    }
}